﻿namespace CSharpPro._0929;
/*using System.text.RegularExpressions;
public class user
{
    public string UserName { get; set; }
    public string Password { get; set; }
}

class System
{
    static List<user> registeredUsers = new List<user>();

    static void usersystem()
    {
        bool exit = false;
        while (!exit)
        {
            Console.WriteLine("主菜单：");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");

            Console.WriteLine("请输入数字:");
            string choiceInput = Console.ReadLine();
            if (!int.TryParse(choiceInput, out int choice) || choice < 1 || choice > 3)
            {
                Console.WriteLine("请输入有效数字！");
                continue;
            }

            switch (choice)
            {
                case 1:
                    Login();
                    break;
                case 2:
                    Register();
                    break;
                case 3:
                    exit = true;
                    Console.WriteLine("程序已退出。");
                    break;
            }
        }
    }

    static void Register()
    {
        Console.WriteLine("=== 用户注册 ===");

        Console.Write("请输入用户名（3 - 15 个字符，只能包含字母、数字、下划线）：");
        string username = Console.ReadLine().Trim();

        if (string.IsNullOrEmpty(username))
        {
            Console.WriteLine("用户名不可为空！");
            return;
        }

        if (username.Length < 3 || username.Length > 15)
        {
            Console.WriteLine("用户名长度必须为 3 - 15 个字符！");
            return;
        }

        if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
        {
            Console.WriteLine("用户名只能包含字母、数字和下划线！");
            return;
        }

        foreach (var user in registeredUsers)
        {
            if (user.UserName == username)
            {
                Console.WriteLine("用户名已存在！");
                return;
            }
        }

        Console.Write("请输入密码（6 - 20 个字符，必须包含至少 1 个大写字母、1 个小写字母和 1 个数字）：");
        string password = Console.ReadLine();

        if (string.IsNullOrEmpty(password))
        {
            Console.WriteLine("密码不可为空！");
            return;
        }

        if (password.Length < 6 || password.Length > 20)
        {
            Console.WriteLine("密码长度必须为 6 - 20 个字符！");
            return;
        }

        if (!Regex.IsMatch(password, @"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).+$"))
        {
            Console.WriteLine("密码必须包含至少 1 个大写字母、1 个小写字母和 1 个数字！");
            return;
        }

        Console.Write("请确认密码：");
        string confirmPassword = Console.ReadLine();

        if (string.IsNullOrEmpty(confirmPassword))
        {
            Console.WriteLine("确认密码不可为空！");
            return;
        }

        if (password != confirmPassword)
        {
            Console.WriteLine("密码与确认密码不一致！");
            return;
        }

        registeredUsers.Add(new user { UserName = username, Password = password });
        Console.WriteLine("注册成功！");
    }

    static void Login()
    {
        Console.WriteLine("=== 用户登录 ===");

        Console.Write("请输入用户名：");
        string username = Console.ReadLine().Trim();

        if (string.IsNullOrEmpty(username))
        {
            Console.WriteLine("用户名不可为空！");
            return;
        }

        Console.Write("请输入密码：");
        string password = Console.ReadLine();

        if (string.IsNullOrEmpty(password))
        {
            Console.WriteLine("密码不可为空！");
            return;
        }

        user user = registeredUsers.Find(u => u.UserName == username);

        if (user == null)
        {
            Console.WriteLine("用户名不存在！");
            return;
        }

        if (user.Password != password)
        {
            Console.WriteLine("密码错误！");
            return;
        }

        Console.WriteLine("登录成功！进入系统首页（模拟）");
    }
}*/
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class User
{
    public string Username { get; set; }
    public string Password { get; set; }
}

class Pro
{
    static List<User> registeredUsers = new List<User>();

    static void main()
    {
        bool exit = false;
        while (!exit)
        {
            Console.WriteLine("主菜单：");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");

            Console.Write("请选择功能（输入数字 1 - 3）：");
            string choiceInput = Console.ReadLine();

            if (!int.TryParse(choiceInput, out int choice) || choice < 1 || choice > 3)
            {
                Console.WriteLine("请输入有效数字！");
                continue;
            }

            switch (choice)
            {
                case 1:
                    Login();
                    break;
                case 2:
                    Register();
                    break;
                case 3:
                    exit = true;
                    Console.WriteLine("程序已退出。");
                    break;
            }
        }
    }

    static void Register()
    {
        Console.WriteLine("=== 用户注册 ===");

        Console.Write("请输入用户名（3 - 15 个字符，只能包含字母、数字、下划线）：");
        string username = Console.ReadLine().Trim();

        if (string.IsNullOrEmpty(username))
        {
            Console.WriteLine("用户名不可为空！");
            return;
        }

        if (username.Length < 3 || username.Length > 15)
        {
            Console.WriteLine("用户名长度必须为 3 - 15 个字符！");
            return;
        }

        if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
        {
            Console.WriteLine("用户名只能包含字母、数字和下划线！");
            return;
        }

        foreach (var user in registeredUsers)
        {
            if (user.Username == username)
            {
                Console.WriteLine("用户名已存在！");
                return;
            }
        }

        Console.Write("请输入密码（6 - 20 个字符，必须包含至少 1 个大写字母、1 个小写字母和 1 个数字）：");
        string password = Console.ReadLine();

        if (string.IsNullOrEmpty(password))
        {
            Console.WriteLine("密码不可为空！");
            return;
        }

        if (password.Length < 6 || password.Length > 20)
        {
            Console.WriteLine("密码长度必须为 6 - 20 个字符！");
            return;
        }

        if (!Regex.IsMatch(password, @"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).+$"))
        {
            Console.WriteLine("密码必须包含至少 1 个大写字母、1 个小写字母和 1 个数字！");
            return;
        }

        Console.Write("请确认密码：");
        string confirmPassword = Console.ReadLine();

        if (string.IsNullOrEmpty(confirmPassword))
        {
            Console.WriteLine("确认密码不可为空！");
            return;
        }

        if (password != confirmPassword)
        {
            Console.WriteLine("密码与确认密码不一致！");
            return;
        }

        registeredUsers.Add(new User { Username = username, Password = password });
        Console.WriteLine("注册成功！");
    }

    static void Login()
    {
        Console.WriteLine("=== 用户登录 ===");

        Console.Write("请输入用户名：");
        string username = Console.ReadLine().Trim();

        if (string.IsNullOrEmpty(username))
        {
            Console.WriteLine("用户名不可为空！");
            return;
        }

        Console.Write("请输入密码：");
        string password = Console.ReadLine();

        if (string.IsNullOrEmpty(password))
        {
            Console.WriteLine("密码不可为空！");
            return;
        }

        User user = registeredUsers.Find(u => u.Username == username);

        if (user == null)
        {
            Console.WriteLine("用户名不存在！");
            return;
        }

        if (user.Password != password)
        {
            Console.WriteLine("密码错误！");
            return;
        }

        Console.WriteLine("登录成功！进入系统首页（模拟）");
    }
}